package com.android.ddfafu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.ddfafu.R;
import com.android.ddfafu.domian.ResultInfo;
import com.android.ddfafu.utils.UserHttpUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class checkemail_activity extends AppCompatActivity {

    private Button yesBtn;
    private Button no_email_btn;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkemail);

        yesBtn = findViewById(R.id.yesbtn);
        no_email_btn = findViewById(R.id.no_email_btn);
        username = getIntent().getStringExtra("username");

        yesBtn.setOnClickListener(v -> {

            if (!StringUtils.isEmpty(username)) {
                String url = "http://101.133.141.41/User/user/checkstatus";
                UserHttpUtil.checkUserStatus(url, username, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String responseData = response.body().string();
                        System.out.println(responseData);
                        ObjectMapper mapper = new ObjectMapper();
                        final ResultInfo info = mapper.readValue(responseData, ResultInfo.class);
                        if (info.isFlag()) {
                            Intent intent = new Intent(checkemail_activity.this, Login_activity.class);
                            startActivity(intent);
                        } else {
                            runOnUiThread(()-> Toast.makeText(checkemail_activity.this, "用户未激活", Toast.LENGTH_SHORT).show());
                        }
                    }
                });
            }
        });
        no_email_btn.setOnClickListener(v -> {
            String url = "http://101.133.141.41/User/user/resend";
            if (!StringUtils.isEmpty(username)) {
                UserHttpUtil.setEmailInForget(url, username, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String responseData = response.body().string();
                        System.out.println(responseData);
                        ObjectMapper mapper = new ObjectMapper();
                        final ResultInfo info = mapper.readValue(responseData, ResultInfo.class);
                        if (info.isFlag()) {
                            runOnUiThread(()-> Toast.makeText(checkemail_activity.this,info.getMsg(),Toast.LENGTH_SHORT).show());

                        } else {
                            runOnUiThread(()-> Toast.makeText(checkemail_activity.this,info.getMsg(),Toast.LENGTH_SHORT).show());
                        }
                    }
                });
            }

        });
    }
}
